package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.DrawRecordAdapter;
import flc.ast.bean.DrawRecordBean;
import flc.ast.databinding.ActivityDrawRecordBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;

public class DrawRecordActivity extends BaseAc<ActivityDrawRecordBinding> {

    private DrawRecordAdapter drawRecordAdapter;
    private List<DrawRecordBean> listShow = new ArrayList<>();
    private List<DrawRecordBean> listDelete = new ArrayList<>();
    private boolean isEdit = false;
    private Dialog myDeleteDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_draw_record;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivDrawRecordBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvDrawRecord.setLayoutManager(new GridLayoutManager(mContext, 3));
        drawRecordAdapter = new DrawRecordAdapter();
        mDataBinding.rvDrawRecord.setAdapter(drawRecordAdapter);
        drawRecordAdapter.setOnItemClickListener(this);

        mDataBinding.ivDrawRecordManage.setOnClickListener(this);
        mDataBinding.ivManageCancel.setOnClickListener(this);
        mDataBinding.ivManageDelete.setOnClickListener(this);
        //删除弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDrawRecordManage:
                isEdit = true;
                drawRecordAdapter.setEdit(isEdit);
                drawRecordAdapter.notifyDataSetChanged();
                mDataBinding.ivDrawRecordManage.setVisibility(View.GONE);
                mDataBinding.llManage.setVisibility(View.VISIBLE);
                break;
            case R.id.ivManageCancel:
                isEdit = false;
                drawRecordAdapter.setEdit(isEdit);
                getData();
                mDataBinding.ivDrawRecordManage.setVisibility(View.VISIBLE);
                mDataBinding.llManage.setVisibility(View.GONE);
                break;
            case R.id.ivManageDelete:
                if (listDelete.size() == 0) {
                    return;
                }
                myDeleteDialog.show();
                break;
            //删除
            case R.id.ivDialogDeleteConfirm:
                for (DrawRecordBean drawRecordBean : listDelete) {
                    FileUtils.delete(drawRecordBean.getPath());
                }
                myDeleteDialog.dismiss();
                isEdit = false;
                drawRecordAdapter.setEdit(isEdit);
                getData();
                mDataBinding.ivDrawRecordManage.setVisibility(View.VISIBLE);
                mDataBinding.llManage.setVisibility(View.GONE);
                break;
            case R.id.ivDialogDeleteCancel:
                myDeleteDialog.dismiss();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (drawRecordAdapter.getItem(position).isSelected()) {
                drawRecordAdapter.getItem(position).setSelected(false);
                listDelete.remove(drawRecordAdapter.getItem(position));
            } else {
                listDelete.add(drawRecordAdapter.getItem(position));
                drawRecordAdapter.getItem(position).setSelected(true);
            }
            drawRecordAdapter.notifyItemChanged(position);
        } else {
            ImgDetailActivity.imgPath = drawRecordAdapter.getItem(position).getPath();
            startActivity(ImgDetailActivity.class);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listDelete.clear();
        listShow.clear();
        List<File> files = FileUtils.listFilesInDir(PathUtils.getFilesPathExternalFirst() + Content.myDraw);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new DrawRecordBean(file.getPath()));
            }
        }
        if (listShow.size() > 0) {
            drawRecordAdapter.setList(listShow);
            mDataBinding.rvDrawRecord.setVisibility(View.VISIBLE);
            mDataBinding.ivDrawRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvDrawRecord.setVisibility(View.GONE);
            mDataBinding.ivDrawRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(true);
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        ImageView comp = inflate.findViewById(R.id.ivDialogDeleteConfirm);
        TextView tip = inflate.findViewById(R.id.tvDialogDeleteTip);
        // 设置文本及其颜色
        StringBuffer str = new StringBuffer();
        str.append(getString(R.string.delete_tip1));
        str.append("<font color='#FF3838'>" + getString(R.string.delete_tip2) + "</font>");
        str.append(getString(R.string.delete_tip3));
        // 在文本控件中显示
        tip.setText(Html.fromHtml(str.toString(), getImageGetter(), null));
        cancel.setOnClickListener(this);
        comp.setOnClickListener(this);
    }

    private Html.ImageGetter getImageGetter() {
        return source -> {
            Drawable drawable = getDrawable(Integer.parseInt(source));
            drawable.setBounds(0, 0, 60, 60);
            return drawable;
        };
    }


}